# -*- coding: utf-8 -*-
########################################################################
#   Pax-Selector. Copyright 2010, Hector Sanjuán.                      #
#                                                                      #
#   Pax-selector is free software: you can redistribute it and/or      #
#   modify it under the terms of the GNU General Public License as     #
#   published by the Free Software Foundation, either version 3 of     #
#   the License, or any later version.                                 #
#                                                                      #
#   Pax-selector is distributed in the hope that it will be useful,    #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of     #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
#   GNU General Public License for more details.                       #
#                                                                      #
#   You should have received a copy of the GNU General Public License. #
#   If not, see <http://www.gnu.org/licenses/>.                        #
#                                                                      #
########################################################################

require 'userInfo'
require 'paxUtils'



class RankingList
  attr_reader :ranking

  def initialize
    @ranking={}
    @gr_sizes=[]
    @users = PaxUtils.usernames
    @condemn_mark = RankingList.adjustedMark("condemn",@users.size)
    @indult_mark = RankingList.adjustedMark("indult",@users.size)
    @users.each do | name |
      user = UserInfo.new(name)
      gr = user.group
      @gr_sizes << gr.size
      gr.each do | id, info |
        unless @ranking[id]
          @ranking[id]={
            :name => info[:name], 
            :rankMark => 0,
            :comments => []
          }
        end	
        mark = info[:mark]
        comment = info[:comment]
        @ranking[id][:rankMark] +=
          RankingList.adjustedMark(mark,@users.size) if mark
        @ranking[id][:comments] << comment.force_encoding("ASCII-8BIT") if comment #hack
      end				
    end
    #randomize comments order
    #set barMarks
    @ranking.each do | id,indiv |
      indiv[:comments].sort!
      if indiv[:rankMark] > @indult_mark then indiv[:barMark] = @indult_mark
      elsif indiv[:rankMark] < @condemn_mark then indiv[:barMark] = @condemn_mark
      else indiv[:barMark] = indiv[:rankMark] end
    end
  end
  
  #TODO CORRECT VOTING SYSTEM TO BE -1 BAD, 0 MEDIUM, +1 GOOD
  def self.adjustedMark(mark_str,nusers)
    case mark_str
    when "condemn"
      r = Float(-(nusers+1))
    when "indult"
      r = Float(nusers+1)
    when "1"
      r = Float(-1.0)
    when "2"
      r = Float(0)
    when "3"
      r = Float(1.0)
    else
      r = Float(0)
    end
    return (r * 100).round.to_f / 100
  end

  def stats
    marks = []
    average = 0
    median = 0
    total_ratio = 0
    condemn_count = 0
    indult_count = 0
    

    @gr_sizes.each {|size| total_ratio+=size}
    total_ratio = Float(total_ratio * 100) / Float(@users.size * @ranking.size)
    total_ratio = (total_ratio *1000).round.to_f / 1000
      

    @ranking.each do | id, indiv |
      mark = indiv[:barMark]
      marks << mark
      average += mark
      condemn_count += 1 if mark <= @condemn_mark
      indult_count += 1 if mark >= @indult_mark      
    end
    average = Float(average) / Float(marks.size)
    average = (average * 1000).round.to_f / 1000
    # median
    marks.sort!
    if marks.size % 1 #impar
      median = marks[marks.size/2]
    else
      median = (marks[(marks.size)/2 - 1] + marks[(marks.size)/2])/2
    end
    {:average => average, 
      :median => median, 
      :condemned => condemn_count, 
      :indulted => indult_count,
      :total_ratio => total_ratio}
  end


end
